﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace Mengsk.ProxyDelivery.Converters
{
    public class VendorConverter : IValueConverter
    {
        private VendorService vendorService = ServiceContainer.GetService<VendorService>();

        private static readonly Dictionary<int, Vendor> vendors = new Dictionary<int, Vendor>();

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }
            int i = int.Parse(value.ToString());
            if (vendors.ContainsKey(i))
            {
                return vendors[i].Name;
            }

            lock (vendors)
            {
                if (vendors.ContainsKey(i) == false)
                {
                    Vendor vendor = this.vendorService.GetById(i);
                    if (vendor == null)
                    {
                        return value;
                    }
                    vendors[i] = vendor;
                    return vendor.Name;
                }
                return "厂家不存在";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
